﻿using System;
using System.Net.Http;
using Microsoft.Extensions.DependencyInjection;
using Masa.Blazor;
using Photino.Blazor;
using TouShuShenQi.BlazorCore.PhotinoAPI;
using PhotinoNET;
using System.Reflection;
using System.Text.Encodings.Web;
using System.Text.Unicode;
using TouShuShenQi.BlazorCore.Repositorys;
using TouShuShenQi.BlazorCore.Models;
using TouShuShenQi.BlazorCore.Services;

namespace TouShuShenQi.BlazorUI
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            var appBuilder = PhotinoBlazorAppBuilder.CreateDefault(args);

           

            var api = new PhotinoApi();
            api.SetHandleHitTest(true);
            

            appBuilder.Services
                
                .AddLogging()
                .AddHttpContextAccessor()
                .AddScoped<BookService>()
                .AddScoped(typeof(BaseRepository<>)) // 注册仓储
                .AddMasaBlazor((builder) =>
                {
                    

                })
                .AddI18nForServer("wwwroot/i18n");

 

            // register root component and selector
            appBuilder.RootComponents.Add<TouShuShenQi.BlazorCore.App>("app");

            var app = appBuilder.Build();

          

            // customize window
            app.MainWindow
                .RegisterApi(api)
                .SetIconFile("favicon.ico")
                .SetTitle("偷书神器")
               .SetContextMenuEnabled(false)
               .SetChromeless(true)
               //.SetGrantBrowserPermissions(false)
                ;

            app.MainWindow.RegisterWindowCreatedHandler((object? sender, EventArgs e) =>
            {
                var window = (PhotinoWindow)sender;

                BookRepository book = new BookRepository();

                //建库
                book.AsSugarClient().DbMaintenance.CreateDatabase();
                //建表
                book.AsSugarClient().CodeFirst.InitTables<BookModel, BookChapterModel>();

             

            });

#if DEBUG
            app.MainWindow.SetDevToolsEnabled(true);
#endif

            AppDomain.CurrentDomain.UnhandledException += (sender, error) =>
            {
                app.MainWindow.OpenAlertWindow("Fatal exception", error.ExceptionObject.ToString());
            };

            app.Run();

        }

         
    }
}
